﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TIBCO.Rendezvous;

namespace TibcoWinform
{
    public partial class RendezvousListen : UserControl
    {
        NetTransport transport;
        Listener mylistener;
        Dispatcher dispatcher;
        Int32 nNumMsgs;
        public RendezvousListen()
        {
            InitializeComponent();
        }
        public void Init()
        {
            TIBCO.Rendezvous.Environment.Open();
            Enable_Create_Transport();
            Disable_Destroy_Transport();
            Txt_Message_Recvd.Text = "";
        }
        public void Destory()
        {
            Btn_Exit_Click(null,null);
        }
        private void Btn_CreateXport_Click(object sender, EventArgs e)
        {
            try
            {
                transport = new NetTransport(Txt_Service.Text, Txt_Network.Text, Txt_Daemon.Text);
                Disable_Create_Transport();
                Enable_Destroy_Transport();
                Enable_Create_Listener();
            }
            catch (RendezvousException ex)
            {

                MessageBox.Show("Unable to Create Transport,  Reason :  " + ex.Message);

            }
        }

        private void Enable_Create_Listener()
        {
            Txt_Subject.Enabled = true;
            Lbl_Subject.Enabled = true;
            Btn_CreateListener.Enabled = true;
        }

        private void Enable_Destroy_Transport()
        {
            Btn_DestroyXport.Enabled = true;
        }

        private void Disable_Create_Transport()
        {
            Txt_Service.Enabled = false;
            Txt_Network.Enabled = false;
            Txt_Daemon.Enabled = false;
            Lbl_Service.Enabled = false;
            Lbl_Network.Enabled = false;
            Lbl_Daemon.Enabled = false;
            Btn_CreateXport.Enabled = false;
        }

        private void Btn_DestroyXport_Click(object sender, EventArgs e)
        {
            Btn_DestroyListener_Click(sender, e);
            if (transport != null)
                transport.Destroy();
            Disable_Destroy_Transport();
            Disable_Create_Listener();
            Enable_Create_Transport();
        }

        private void Enable_Create_Transport()
        {
            Txt_Service.Enabled = true;
            Txt_Network.Enabled = true;
            Txt_Daemon.Enabled = true;
            Txt_Subject.Enabled = true;
            Lbl_Service.Enabled = true;
            Lbl_Network.Enabled = true;
            Lbl_Daemon.Enabled = true;
            Lbl_Subject.Enabled = true;

            Btn_CreateXport.Enabled = true;

            Btn_CreateListener.Enabled = false;
            Btn_DestroyListener.Enabled = false;
        }

        private void Disable_Create_Listener()
        {
            Txt_Subject.Enabled = false;
            Lbl_Subject.Enabled = false;
            Btn_CreateListener.Enabled = false;
        }

        private void Disable_Destroy_Transport()
        {
            Btn_DestroyXport.Enabled = false;
        }

        private void Btn_Exit_Click(object sender, EventArgs e)
        {
            Btn_DestroyListener_Click(this, e);
            TIBCO.Rendezvous.Environment.Close();
        }

        private void Btn_CreateListener_Click(object sender, EventArgs e)
        {
            nNumMsgs = 0;

            try
            {
                if (Txt_Subject.Text == "")
                    Txt_Message_Recvd.Text += "Please enter a valid subject name ...";
                mylistener = new Listener(Queue.Default, transport, Txt_Subject.Text, 0);
                dispatcher = new Dispatcher(Queue.Default);
                mylistener.MessageReceived += OnMessageReceived;
                Txt_MsgCount.Text = "0";
                Disable_Create_Listener();
                Enable_Destroy_Listener();
            }
            catch (TIBCO.Rendezvous.RendezvousException ex)
            {
                MessageBox.Show("Unable to Create Listener,  Reason :  " + ex.Message);
            }
        }

        private void Enable_Destroy_Listener()
        {
            Btn_DestroyListener.Enabled = true;
        }

        private void OnMessageReceived(object listener, MessageReceivedEventArgs messageReceivedEventArgs)
        {
            TIBCO.Rendezvous.Message message;
            string msgdata = "";
            nNumMsgs = nNumMsgs + 1;
            message = messageReceivedEventArgs.Message;
            SerializableHelper.Save2(message.ToString(),"ReceiveDataLog.json");
            msgdata = $"{msgdata}{DateTime.Now.ToString()}{message.SendSubject}{ message.ReplySubject}{message.ToString()}";
            ControlHelper.InvokeIfNeeded(this, new Action(() =>
            {
                Txt_MsgCount.Text = nNumMsgs.ToString();
                Txt_Message_Recvd.Text = $"{Txt_Message_Recvd.Text}\r\nIn OnMessageReceived ...";
                Txt_Message_Recvd.Text = $"{Txt_Message_Recvd.Text}\r\n{msgdata}\r\n";
                Txt_Message_Recvd.SelectionStart = Txt_Message_Recvd.TextLength;
                Txt_Message_Recvd .ScrollToCaret();
            }));

        }

        private void Btn_DestroyListener_Click(object sender, EventArgs e)
        {
            if (mylistener != null)
                mylistener.Destroy();
            mylistener = null;
            Disable_Destroy_Listener();
            Enable_Create_Listener();
        }

        private void Disable_Destroy_Listener()
        {
            Btn_DestroyListener.Enabled = false;
        }

        private void Btn_ClearMsgs_Click(object sender, EventArgs e)
        {
            Txt_Message_Recvd.Clear();
        }

    }
}
